#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6 + 10;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
ll p[N];
ll n, m, k, a, b;
ll fd(ll x)
{
    if(p[x]!=x)
    {
        p[x] = fd(p[x]);
    }
    else
        p[x] = x;
    return p[x];
}
void solve()
{
    cin >> n >> m;
    cin >> k;
    for (ll i = 1; i <= n * m;i++)
    {
        p[i] = i;
    }
        for (ll i = 0; i < k; i++)
        {
            cin >> a >> b;
            ll pa = fd(a), pb = fd(b);
            if (pa != pb)
            {
                p[pa] = pb;
            }
        }
    ll ans = 0;
    for (ll i = 1; i <= n * m;i++)
    {
        if(p[i]==i)
        {
            ans++;
        }
    }
    cout << ans;
}
int main()
{
    ll t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}